
/* JavaScript content from map/classes/MapView.js in folder common */
define(['dojo',
  'map/classes/Utils',
  'dojo/domReady!'], function(dojo, Utils){
  return dojo.declare("map.mapView", [],
  {
    constructor : function (mapNode) {
      this.mapElName = "olMap";

      this.MIN_ZOOM = 12;
      this.MAX_ZOOM = 18;      
    },
    init : function(elName) {

      var el = elName || this.mapElName;

      this.olMap = new OpenLayers.Map(el, { units: 'm', projection: "EPSG:4326" });

      this.layer = new OpenLayers.Layer.Google("MAP", {numZoomLevels: 20}, { transitionEffect: 'resize' });
      this.layer2 = new OpenLayers.Layer.Google("SATELLITE", {type: google.maps.MapTypeId.SATELLITE, numZoomLevels: 20}, { transitionEffect: 'resize' });

      this.markers = new OpenLayers.Layer.Markers( "Markers" );
      
      this.olMap.addLayer(this.markers);

      this.olMap.addLayer(this.layer);

      this.satelliteLayer = false;

      var longitude = window.currentPosition.coords.longitude;
      var latitude = window.currentPosition.coords.latitude;
      var defaultZoom = 15.5;

      this.olMap.setCenter(Utils.toOL(new OpenLayers.LonLat(longitude, latitude)), defaultZoom);
      
      $("#"+this.mapElName).on('mousedown touchstart', this.pointerStart.bind(this));
      $("#"+this.mapElName).on('mousemove touchmove', this.pointerMove.bind(this));
      

    },
    last:{
      x : 0,
      y : 0
    },
    pointerStart:function(evt){
//      console.log(evt);

      var pageX = evt.pageX
        , pageY = evt.pageY;

      this.last = {
        x: pageX,
        y: pageY
      };

//      console.log(this.last);
    },
    pointerMove:function(evt){

      var pageX = evt.pageX
        , pageY = evt.pageY;

      var delta = {
        x: pageX - this.last.x,
        y: pageY - this.last.y
      };

      this.olMap.pan(-delta.x, -delta.y, { dragging: true });

      this.last.x = pageX;
      this.last.y = pageY;
    },
    resize: function(){
      return false;
    },
    isSatView: function() {
      return this.satelliteLayer;
    },
    switchLayer: function () {
      if (this.isSatView()) {
        this.olMap.removeLayer(this.layer2);
        this.olMap.addLayer(this.layer);
        this.satelliteLayer = false;
      } else {
        this.olMap.removeLayer(this.layer);
        this.olMap.addLayer(this.layer2);
        this.satelliteLayer = true;
      }
    },
    zoomLeveL: function(){
      this.olMap.getZoom();
    },
    zoomOut: function() {
      this.olMap.zoomOut();
    },
    zoomIn: function() {
      this.olMap.zoomIn();
    }
  });
});